<!DOCTYPE html>
<html>
<head>
	<link href="styles.css" rel="stylesheet" type="text/css">
	<title>Edit Deposit</title>
</head>
<body>
	
	<?php
		// include required files
		require_once ("constants.php");
		require_once ("deposits_class.php");

		if ($_SERVER['REQUEST_METHOD'] === 'POST') {
			$view = $_POST['planet_view'];
			$planet_id = $_POST['planet_id'];
			$planet_name = $_POST['planet_name'];
			$planet_size = $_POST['planet_size'];
			$dep_id = $_POST['dep_id'];
		}
									
		// if the deposit id is defined, create the input forms	
		if(isset($dep_id)){

			// html header
			echo "<h1 align=\"center\">Edit Deposit</h1>";
			echo "<h2 align=\"center\">Planet Name: ".$planet_name."</h2>\n";

			$deposit = new deposits($dep_id);
			
			// retrieves deposit data from the database
			$data = $deposit->DepositDetail();
			
			// if there is data
			if($data){
				
				// if we are working in "map" view, creates a "delete deposit" button
				if($view === "map"){
					
					echo "<br><p><FORM METHOD=\"post\" ACTION=\"mapDepos.php\" style=\"margin-left: auto; margin-right: auto; text-align: center;\">\n";
					echo "<input type=\"hidden\" name=\"db_action\" value=\"delete\">\n";
					echo "<input type=\"hidden\" name=\"dep_id\" value=".$dep_id.">";
					echo "<input type=\"hidden\" name=\"planet_id\" value=".$planet_id.">\n";
					echo "<input type=\"submit\" value=\"Delete Deposit\" name=\"editar\">\n";
					echo "</FORM></p>\n";
				}	
				
				// displays the deposit data				
				echo "<p><b>Position : </b>(".$data['x_coord'].", ".$data['y_coord'].")<br/>";
				echo "<b>Material : </b>".$data['material']."<br/>";
				echo "<b>Size     : </b>".$data['size']."<br/>";
				echo "<b>User     : </b>".$data['user']."<br/>";
				echo "<b>Timestamp: </b>".$data['time_mod']."</p>";
				
				// creates the update form, redirects to mapDepos if we are in "map" view or listDepos if not
				if($view === "map"){
					echo "<FORM METHOD=\"post\" ACTION=\"mapDepos.php\">";
				}
				else {
					echo "<FORM METHOD=\"post\" ACTION=\"listDepos.php\">";
				}
				echo "<input type=\"hidden\" name=\"db_action\" value=\"update\">\n";
				echo "<input type=\"hidden\" name=\"dep_id\" value=".$dep_id.">";
				echo "<input type=\"hidden\" name=\"planet_id\" value=".$planet_id.">";
				echo "<input type=\"hidden\" name=\"planet_name\" value=".$planet_name.">";
				echo "<input type=\"hidden\" name=\"planet_size\" value=".$planet_size.">";
				echo "<p>New Size <input type=\"text\" name=\"dep_size\" size=\"30\" value=\"".$data['size']."\"></p>";
				echo "<p>New Material <select name=\"material\" size=\"1\">";
				
				// create the materials dropbox with the mats in the $rm_array (in constants.php)
				foreach($rm_array as $mat){
					if($mat == $data['material']){
						echo "<option selected>".$mat."</option>";
					} else {
						echo "<option>".$mat."</option>";
					}
				}
				echo "</select></p>";
				echo "<input type=\"submit\" value=\"Edit\" name=\"submit\">";
				echo "</FORM>";
				
			}
			else {
				// no deposit data error
				echo "<p align=\"center\"><strong>No deposit data.</strong></p>";
			}		

		}
		
		echo "<br>";

		// creates a "back" button to the working view
		if($view === "map"){
			echo "<FORM class=\"bar\" METHOD=\"post\" ACTION=\"mapDepos.php\">\n";
			echo "<input type=\"hidden\" name=\"planet_id\" value=".$planet_id.">\n";
			echo "<input type=\"submit\" value=\"Back to Map View\" name=\"editar\">\n";
			echo "</FORM><br>\n";
		}
		else
		{
			echo "<FORM class=\"bar\" METHOD=\"post\" ACTION=\"listDepos.php\">\n";
			echo "<input type=\"hidden\" name=\"planet_id\" value=".$planet_id.">\n";
			echo "<input type=\"submit\" value=\"Back to List View\" name=\"editar\">\n";
			echo "</FORM><br>\n";
		}
	?>

	<br>
	<p align="center"><a href="index.php">Back to List of Planets</a></p>
	
</body>
</html>
